package inet.app.view.activity;

import inet.app.R;
import inet.app.common.XoSoConstants;
import inet.app.controller.request.Request;
import inet.app.view.adapter.KetQuaDetailAdapter;
import inet.app.view.fragment.FragmentLmtMN;
import inet.app.view.fragment.FragmentLmtMT;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LichMoThuongFragment extends BaseFragmentActivity implements
		OnClickListener {
	
	protected Request request = new Request();
	private ViewPager _mViewPager;
	private KetQuaDetailAdapter _adapter;
	private FragmentLmtMT fmt;
	private FragmentLmtMN fmn;
	int Yellow1 = Color.rgb(255, 255, 0);
	int Red1 = Color.rgb(255, 0, 0);
	int heavyblue = Color.rgb(0, 0, 255);
	private ArrayList<Fragment> list_Fragment;
	public int posCurrent = 0;
	Dialog about, help, dialog;
	int demtrang = 1;
	int max = 10;
	private TextView tvTitle;
	private LinearLayout first_text, second_text;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.lichmothuong_fragment);
		initBase();
		initUI();
		initFragment();
		init_ViewPager();
		setTab();
	}

	private void initUI() {
		first_text = (LinearLayout) findViewById(R.id.first_text);
		second_text = (LinearLayout) findViewById(R.id.second_text);
		first_text.setOnClickListener(this);
		second_text.setOnClickListener(this);

		tvTitle = (TextView) findViewById(R.id.tvTitle);
		_mViewPager = (ViewPager) findViewById(R.id.viewPager);
		list_Fragment = new ArrayList<Fragment>();
	}

	private void initFragment() {
		fmt = (FragmentLmtMT) FragmentLmtMT.newInstance(this);
		fmn = (FragmentLmtMN) FragmentLmtMN.newInstance(this);
		first_text.setBackgroundResource(R.drawable.tk_tab1_2);
		second_text.setBackgroundResource(R.drawable.tk_tab2);
		tvTitle.setText("Lịch Mở Thưởng");
		list_Fragment.add(fmt);
		list_Fragment.add(fmn);

	}

	public void init_ViewPager() {
		FragmentManager fm = getSupportFragmentManager();
		_adapter = new KetQuaDetailAdapter(this, _mViewPager, fm, list_Fragment);
		_mViewPager.setAdapter(_adapter);
		_mViewPager.setCurrentItem(posCurrent);

		_mViewPager.setCurrentItem(0);
		first_text.setBackgroundResource(R.drawable.tk_tab1_2);
		second_text.setBackgroundResource(R.drawable.tk_tab2);
		tvTitle.setText("Lịch Mở Thưởng");
	}

	private void setTab() {
		_mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int position) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			@Override
			public void onPageSelected(int position) {
				switch (position) {
				case 0:
					posCurrent = 0;
					fmt = (FragmentLmtMT) _adapter.getItem(0);
					first_text
							.setBackgroundResource(R.drawable.tk_tab1_2);
					second_text.setBackgroundResource(R.drawable.tk_tab2);
					_mViewPager.setCurrentItem(0);
					tvTitle.setText("Lịch Mở Thưởng");
					break;
				case 1:
					posCurrent = 1;
					fmn = (FragmentLmtMN) _adapter.getItem(1);
					first_text.setBackgroundResource(R.drawable.tk_tab1);
					second_text
							.setBackgroundResource(R.drawable.tk_tab2_2);
					tvTitle.setText("Lịch Mở Thưởng");
					_mViewPager.setCurrentItem(1);
					break;
				}
			}

		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.first_text:
			posCurrent = 0;
			fmt = (FragmentLmtMT) _adapter.getItem(0);
			first_text.setBackgroundResource(R.drawable.tk_tab1_2);
			second_text.setBackgroundResource(R.drawable.tk_tab2);
			_mViewPager.setCurrentItem(0);
			tvTitle.setText("Lịch Mở Thưởng");
			break;
		case R.id.second_text:
			posCurrent = 1;
			fmn = (FragmentLmtMN) _adapter.getItem(1);
			first_text.setBackgroundResource(R.drawable.tk_tab1);
			second_text.setBackgroundResource(R.drawable.tk_tab2_2);
			tvTitle.setText("Lịch Mở Thưởng");
			_mViewPager.setCurrentItem(1);
			break;

		default:
			break;
		}
	}

	@Override
	public void onResume() {
		if (((MyApp) getApplication()).isKill()) {
			finish();
		}
		super.onResume();

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			System.out.println("THOAT NHE");
			quitDialog(LichMoThuongFragment.this);
			dlquit.show();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	public void quitDialog(Context c) {
		dlquit = new Dialog(c);
		dlquit.requestWindowFeature(Window.FEATURE_LEFT_ICON);
		dlquit.setTitle("Thoát ứng dụng ?");
		dlquit.setContentView(R.layout.dialog_quit);
		dlquit.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		dlquit.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.rgb(50, 50, 50)));
		dlquit.getWindow().setTitleColor(XoSoConstants.White);
		dlquit.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.icon_registersc);
		
		Button huy = (Button) dlquit.findViewById(R.id.bthuy);
		huy.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				dlquit.dismiss();
			}
		});

		Button quit = (Button) dlquit.findViewById(R.id.btquit);
		quit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				((MyApp) getApplication()).setKill(true);
				finish();
				dlquit.dismiss();
			}
		});
	}
}
